package net.zdreamer.geracomium.ui.more;

import net.zdreamer.geracomium.application.BaseApplication;
import net.zdreamer.geracomium.myviews.CustomProgressDialog;
import net.zdreamer.geracomium.ui.BaseActivity;
import net.zdreamer.geracomium.utils.PersonalinfoUtils.IdcardUtils;
import net.zdreamer.geracomium.utils.file.SharePreferencesUtil;
import net.zdreamer.geracomium.utils.httputils.PersonalCenterService;
import net.zdreamer.geracomium.utils.tools_utils.URLTools;

import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.geracomium.R;

import java.util.HashMap;
import java.util.Map;

public class GuardianActivity extends BaseActivity implements OnClickListener {

    private LinearLayout guardain_name, guardain_sex, guardain_age,
            guardain_idCard, guardain_live_address, guardain_relationships,
            guardain_Contact_info;
    private PopupWindow pop, pop_sex;
    private View sex_view;
    private TextView no_choose, sex_choose,
            info_live_address, guardain_id, guardain_infoage,
            guardain_infoname, tv_elderbeddinfo, relationToOldPerson, phoneNo, tv_commite;
    private RelativeLayout man_choose, woman_choose;
    private ImageView image_man, image_woman, tv_oldman;
    private Intent intent;
private Dialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        // TODO Auto-generated method stub
        return R.layout.activity_guardian;
    }

    @Override
    protected void init() {
        guardain_name = (LinearLayout) findViewById(R.id.guardain_name);
        guardain_sex = (LinearLayout) findViewById(R.id.guardain_sex);
        guardain_age = (LinearLayout) findViewById(R.id.guardain_age);
        guardain_idCard = (LinearLayout) findViewById(R.id.guardain_idCard);
        guardain_live_address = (LinearLayout) findViewById(R.id.guardain_live_address);
        guardain_relationships = (LinearLayout) findViewById(R.id.guardain_relationships);
        guardain_Contact_info = (LinearLayout) findViewById(R.id.guardain_Contact_info);

        sex_choose = (TextView) findViewById(R.id.sex_choose);
        info_live_address = (TextView) findViewById(R.id.guardain_address);
        guardain_id = (TextView) findViewById(R.id.guardain_id);
        guardain_infoage = (TextView) findViewById(R.id.guardain_infoage);
        guardain_infoname = (TextView) findViewById(R.id.guardain_infoname);
        tv_elderbeddinfo = (TextView) findViewById(R.id.tv_elderbeddinfo);
        relationToOldPerson = (TextView) findViewById(R.id.relationToOldPerson);
        phoneNo = (TextView) findViewById(R.id.phoneNo);
        tv_commite = (TextView) findViewById(R.id.tv_guardcommit);


        loadingDialog= CustomProgressDialog.createLoadingDialog(this,"提交中……");
    }

    @Override
    protected void initViews() {
        // TODO Auto-generated method stub
        initTitle("个人资料", R.drawable.selector_title_back,
                R.drawable.selector_title_user, true, true);
        Intent intent = getIntent();
        info_live_address.setText(intent.getStringExtra("居住地址"));
        setSexPopuwindow();
    }

    @Override
    protected void initListener() {
        // TODO Auto-generated method stub
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.share).setOnClickListener(this);
        guardain_name.setOnClickListener(this);
        guardain_sex.setOnClickListener(this);
        guardain_age.setOnClickListener(this);
        guardain_idCard.setOnClickListener(this);
        guardain_live_address.setOnClickListener(this);
        guardain_relationships.setOnClickListener(this);
        guardain_Contact_info.setOnClickListener(this);

        tv_elderbeddinfo.setOnClickListener(this);
        tv_commite.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back:
                Intent back_personal_center = new Intent(GuardianActivity.this,
                        PersonalCenterActivity.class);
                startActivity(back_personal_center);
                GuardianActivity.this.finish();
                break;
            case R.id.guardain_name:
                intent = new Intent(GuardianActivity.this,
                        PersonalInformationEditActivity.class);
                intent.putExtra("info", "姓名");
                startActivityForResult(intent, 0);
                break;
            case R.id.guardain_sex:
                pop_sex.showAtLocation(v, Gravity.CENTER, 0, getStatusBarHeight());
                break;
            case R.id.guardain_idCard:
                intent = new Intent(GuardianActivity.this,
                        PersonalInformationEditActivity.class);
                intent.putExtra("info", "身份证");
                startActivityForResult(intent, 0);
                break;
            case R.id.guardain_live_address:
                Intent choose_native = new Intent(GuardianActivity.this,
                        ChooseAdressActivity.class);
                choose_native.putExtra("info", "居住地址");
                choose_native.putExtra("source", "guardian");
                startActivity(choose_native);
                // Guardian_Activity.this.finish();
                break;
            case R.id.guardain_relationships:
                intent = new Intent(GuardianActivity.this,
                        PersonalInformationEditActivity.class);
                intent.putExtra("info", "与老人关系");
                startActivityForResult(intent, 0);
                break;
            case R.id.guardain_Contact_info:
                intent = new Intent(GuardianActivity.this,
                        PersonalInformationEditActivity.class);
                intent.putExtra("info", "联系方式");
                startActivityForResult(intent, 0);
                break;
            case R.id.share:
                Intent userinfo = new Intent(GuardianActivity.this, PersonalInformationActivity.class);
                startActivity(userinfo);
                break;
            case R.id.no_choose:
                pop_sex.dismiss();
                break;
            case R.id.man_choose:
                image_man.setVisibility(View.VISIBLE);
                image_woman.setVisibility(View.GONE);
                sex_choose.setText("男");
                pop_sex.dismiss();
                break;
            case R.id.woman_choose:
                image_man.setVisibility(View.GONE);
                image_woman.setVisibility(View.VISIBLE);
                sex_choose.setText("女");
                pop_sex.dismiss();
                break;
            case R.id.tv_elderbeddinfo:
                Intent elder = new Intent(GuardianActivity.this, ElderInfofmationActivity.class);
                startActivity(elder);
                GuardianActivity.this.finish();
                break;
            case R.id.tv_guardcommit:
                Submit();
              //  System.out.println("点击提交");
                break;
            default:
                break;
        }
    }

    /**
     * 提交数据
     */

    public void Submit() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("guardianId", BaseApplication.get().getUserId());
        map.put("name", guardain_infoname.getText());
        map.put("sex", sex());
        map.put("age", Integer.parseInt(guardain_infoage.getText().toString()));
        map.put("idNo", guardain_id.getText());
        map.put("residenceAddress", info_live_address.getText());
        map.put("relationToOldPerson", relationToOldPerson.getText());
        map.put("phoneNo", phoneNo.getText());
        PersonalCenterService.RegistPost(map, URLTools.GuardianUrl, handler);
        SharePreferencesUtil.Writer(this,"GuardianInfo",map);
        loadingDialog.show();
    }

    public String sex() {
        if (sex_choose.getText().equals("男")) {
            return "Male";
        } else {
            return "Female";
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.arg1) {
                case 201:
                    System.out.println(msg.obj);
                    loadingDialog.dismiss();
                    Toast.makeText(GuardianActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                    break;
                case 401:
                    loadingDialog.dismiss();
                    Toast.makeText(GuardianActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String change01;
            if (data.getStringExtra("姓名") != null) {
                change01 = data.getStringExtra("姓名");
                guardain_infoname.setText(change01);
            } else if (data.getStringExtra("身份证") != null) {
                change01 = data.getStringExtra("身份证");
                guardain_id.setText(change01);
            } else if (data.getStringExtra("与老人关系") != null) {
                change01 = data.getStringExtra("与老人关系");
                relationToOldPerson.setText(change01);
            } else if (data.getStringExtra("联系方式") != null) {
                change01 = data.getStringExtra("联系方式");
                phoneNo.setText(change01);

            }
        }
        getBrithDate();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (EditAdressActivity.edit_index != null) {
            if (EditAdressActivity.edit_index.equals("居住地址")) {
                info_live_address.setText(EditAdressActivity.back_address);
            }
        }
    }

    /**
     * 获取生日年龄
     */
    private void getBrithDate() {
        String brith = guardain_id.getText().toString().trim();
        if (brith != "") {
            guardain_infoage.setText(String.valueOf(IdcardUtils.getAge(brith)));
        }
    }


    /**
     * 选择性别
     */
    private void setSexPopuwindow() {
        // TODO
        LayoutInflater inflater = LayoutInflater.from(this);
        sex_view = inflater.inflate(R.layout.pop_sex, null);
        pop_sex = new PopupWindow(sex_view, LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        pop_sex.setBackgroundDrawable(new ColorDrawable(0x77000000));
        pop_sex.setOutsideTouchable(true);
        pop_sex.setFocusable(true);

        no_choose = (TextView) sex_view.findViewById(R.id.no_choose);
        man_choose = (RelativeLayout) sex_view.findViewById(R.id.man_choose);
        woman_choose = (RelativeLayout) sex_view
                .findViewById(R.id.woman_choose);
        image_man = (ImageView) sex_view.findViewById(R.id.image_man);
        image_woman = (ImageView) sex_view.findViewById(R.id.image_woman);
        man_choose.setOnClickListener(this);
        woman_choose.setOnClickListener(this);
        no_choose.setOnClickListener(this);
    }

    /**
     * popwindow覆盖全屏
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
